package cn.shangqizhenshuai.core;

import cn.shangqizhenshuai.entity.JavaEntity;
import cn.shangqizhenshuai.entity.MapperEntity;
import cn.shangqizhenshuai.entity.TableEntity;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ServiceExecuteCore extends ExecuteCore {
    public  String  serviceimolftl = null;

    @Override
    public HashMap<String, List> execute(HashMap<String, List> context) throws IOException, TemplateException {

        List<MapperEntity> mapentity = context.get("mapperinfo");
        ArrayList<String> servicepackage = new ArrayList<String>();

        //配置模板文件对象
        Configuration conf=new Configuration();
        //导入模板目录
        File file = new File("src/main/resources/");
        conf.setDirectoryForTemplateLoading(file);
        Template temp = conf.getTemplate(this.getFTLname());
        //开始文件输出
        //开始文件输出
        String packagename = this.getPackagename();
        String[] split = packagename.split("\\.");
        String outpath = "src/main/java/"+this.getOutFilePath();
        for (String s : split) {
            outpath +="/"+s;
        }
        File file1 = new File(outpath);
        file1.mkdirs();
        //获取mapp接口地址
        String mapperip=null;
        List<MapperEntity> mapperEntities = new ArrayList<MapperEntity>();
        for (MapperEntity mapperEntity : mapentity) {
            mapperip= mapperEntity.getImPackage();
            mapperEntity.setImPackage(this.getPackagename());
            //开始循环输出文件
            Writer out=new FileWriter(new File(outpath+"/"+mapperEntity.getJavaname()+"Service.java"),true);
            temp.process(mapperEntity, out);
            out.flush();
            out.close();
        }
        //增加impl实现
        Configuration conf2=new Configuration();
        conf2.setDirectoryForTemplateLoading(file);
        Template temp2 = conf2.getTemplate(this.getServiceimolftl());
        String[] split2 = packagename.split("\\.");
        String outpath2 ="src/main/java/"+this.getOutFilePath();
        System.out.println("========================>"+outpath2);

        for (String s : split2) {
            outpath2 +="/"+s;
        }
        outpath2+="/impl";
        System.out.println("========================>"+outpath2);
        File file2 = new File(outpath2);
        file2.mkdirs();
        for (MapperEntity mapperEntity : mapentity) {
            String imPackage = mapperEntity.getImPackage();
            List<String> imports = mapperEntity.getImports();
            imports.add(mapperip+"."+mapperEntity.javaname+"Mapper");
            imports.add(imPackage+"."+mapperEntity.javaname+"Service");
            servicepackage.add(this.getPackagename());
            mapperEntity.setImports(imports);
            mapperEntity.setImPackage(this.getPackagename()+".impl");
            //开始循环输出文件
            Writer out=new FileWriter(new File(outpath2+"/"+mapperEntity.getJavaname()+"ServiceImpl.java"),true);
            temp2.process(mapperEntity, out);
            out.flush();
            out.close();
        }
        context.put("servicepackage",servicepackage);
        return context;
    }

    public String getServiceimolftl() {
        return serviceimolftl;
    }

    public void setServiceimolftl(String serviceimolftl) {
        this.serviceimolftl = serviceimolftl;
    }


}
